/*
* Copyright (c) 2020
*
* Change Logs:
* Date           Author       Notes
* 2020-08-06     chao         the first version
*/

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"

#include "simple_key.h"

/****    Debug     ****/
#define DBG_ENABLE
#define DBG_SECTION_NAME "key"
#define DBG_LEVEL        DBG_INFO
#define DBG_COLOR
#include <rtdbg.h>

#ifdef BSP_USING_SIMPLE_BUTTON

#define KEY_U_PIN           0 //GET_PIN(A, 0)
#define KEY_U_PRESS_LEVEL   0

#define KEY_D_PIN           67//GET_PIN(E, 3)
#define KEY_D_PRESS_LEVEL   0

#define KEY_L_PIN           66//GET_PIN(E, 2)
#define KEY_L_PRESS_LEVEL   1

#define KEY_R_PIN           68//GET_PIN(E, 4)
#define KEY_R_PRESS_LEVEL   0

struct my_button btn_up = {0}, btn_down = {0}, btn_left = {0}, btn_right = {0};

/*
 * @brief
 * @param
 * @retval
 */
static void key_click_down(rt_uint32_t pin)
{
    switch (pin)
    {
    case KEY_U_PIN:
        LOG_I("[up] click down");
        break;
        
    case KEY_D_PIN:
        LOG_I("[down] click down");
        break;
        
    case KEY_L_PIN:
        LOG_I("[left] click down");
        break;
        
    case KEY_R_PIN:
        LOG_I("[right] click down");
        break;
    }
}

/*
 * @brief
 * @param
 * @retval
 */
static void key_click_up(rt_uint32_t pin)
{
    switch (pin)
    {
    case KEY_U_PIN:
        LOG_I("[up] click up");
        break;
        
    case KEY_D_PIN:
        LOG_I("[down] click up");
        break;
        
    case KEY_L_PIN:
        LOG_I("[left] click up");
        break;
        
    case KEY_R_PIN:
        LOG_I("[right] click up");
        break;
    }
}

/*
 * @brief
 * @param
 * @retval
 */
static void key_hold_tri(rt_uint32_t pin)
{
    switch (pin)
    {
    case KEY_U_PIN:
        LOG_I("[up] hold tri");
        break;
        
    case KEY_D_PIN:
        LOG_I("[down] hold tri");
        break;
        
    case KEY_L_PIN:
        LOG_I("[left] hold tri");
        break;
        
    case KEY_R_PIN:
        LOG_I("[right] hold tri");
        break;
    }
}

/*
 * @brief
 * @param
 * @retval
 */
static void key_hold_cyc(rt_uint32_t pin)
{
    switch (pin)
    {
    case KEY_U_PIN:
        LOG_I("[up] hold cyc");
        break;
        
    case KEY_D_PIN:
        LOG_I("[down] hold cyc");
        break;
        
    case KEY_L_PIN:
        LOG_I("[left] hold cyc");
        break;
        
    case KEY_R_PIN:
        LOG_I("[right] hold cyc");
        break;
    }
}

/*
 * @brief
 * @param
 * @retval
 */
static void key_hold_up(rt_uint32_t pin)
{
    switch (pin)
    {
    case KEY_U_PIN:
        LOG_I("[up] hold up");
        break;
        
    case KEY_D_PIN:
        LOG_I("[down] hold up");
        break;
        
    case KEY_L_PIN:
        LOG_I("[left] hold up");
        break;
        
    case KEY_R_PIN:
        LOG_I("[right] hold up");
        break;
    }
}

/*
 * @brief
 * @param
 * @retval
 */
void btn_cb(struct my_button *button)
{
    switch (button->event)
    {
    case BUTTON_EVENT_CLICK_DOWN:
        key_click_down(button->pin);
        break;
    case BUTTON_EVENT_CLICK_UP:
        key_click_up(button->pin);
        break;
    case BUTTON_EVENT_HOLD_TRI:
        key_hold_tri(button->pin);
        break;
    case BUTTON_EVENT_HOLD_CYC:
        key_hold_cyc(button->pin);
        break;
    case BUTTON_EVENT_HOLD_UP:
        key_hold_up(button->pin);
        break;
    default:
        ;
    }
}

/*
 * @brief
 * @param
 * @retval
 */
int key_init(void)
{
    btn_up.pin = KEY_U_PIN;
    btn_up.press_logic_level = KEY_U_PRESS_LEVEL;
    btn_up.hold_tri_time = MY_BUTTON_HOLD_TRI_MS;
    btn_up.hold_cyc_period = MY_BUTTON_HOLD_CYC_MS;
    btn_up.cb = (my_button_callback)btn_cb;

    btn_down.pin = KEY_D_PIN;
    btn_down.press_logic_level = KEY_D_PRESS_LEVEL;
    btn_down.hold_tri_time = MY_BUTTON_HOLD_TRI_MS;
    btn_down.hold_cyc_period = MY_BUTTON_HOLD_CYC_MS;
    btn_down.cb = (my_button_callback)btn_cb;
    
    btn_left.pin = KEY_L_PIN;
    btn_left.press_logic_level = KEY_L_PRESS_LEVEL;
    btn_left.hold_tri_time = MY_BUTTON_HOLD_TRI_MS;
    btn_left.hold_cyc_period = MY_BUTTON_HOLD_CYC_MS;
    btn_left.cb = (my_button_callback)btn_cb;
    
    btn_right.pin = KEY_R_PIN;
    btn_right.press_logic_level = KEY_R_PRESS_LEVEL;
    btn_right.hold_tri_time = MY_BUTTON_HOLD_TRI_MS;
    btn_right.hold_cyc_period = MY_BUTTON_HOLD_CYC_MS;
    btn_right.cb = (my_button_callback)btn_cb;

    my_button_register(&btn_up);
    my_button_register(&btn_down);
    my_button_register(&btn_left);
    my_button_register(&btn_right);
    my_button_start();

    return 0;
}
INIT_DEVICE_EXPORT(key_init);

#endif /* BSP_USING_SIMPLE_BUTTON */


